<script setup lang="ts">
import {PlayStatus, useAudioPlayerStore} from "@/stores"
import Logo from '@/assets/logo.png'
import { onMounted, watch, ref, nextTick } from 'vue'

const audioPlayer = useAudioPlayerStore()

const armPlaying = ref(false)
const armPlayed = ref(false)

const transFromStyle = ref("rotate(15deg)")

onMounted(()=>{
  handleStatus()
})


watch(()=>audioPlayer.status, ()=>{
  handleStatus()
})

const handleStatus = ()=>{
  switch (audioPlayer.status){
    case PlayStatus.playing:
      handlePlayAnimation()
      armPlay()
      break;
    default:
      handlePauseAnimation()
      armStop()
      break;
  }
}


const handlePlayAnimation = () =>{
  const album = document.querySelector("img.album_cover")
  if(album)
  {
    album.style.animationPlayState = 'running'
  }


  const record = document.querySelector("img.record")
  if(record)
  {
    record.style.animationPlayState = 'running'
  }

  const record_high = document.querySelector("img.record_higher")
  if(record_high)
  {
    record_high.style.animationPlayState = 'running'
  }

  switchOn()
}

const handlePauseAnimation = () =>{
  const album = document.querySelector("img.album_cover")
  if(album)
  {
    album.style.animationPlayState = 'paused'
  }

  const record = document.querySelector("img.record")
  if(record)
  {
    record.style.animationPlayState = 'paused'
  }

  const record_high = document.querySelector("img.record_higher")
  if(record_high)
  {
    record_high.style.animationPlayState = 'paused'
  }
  switchOff()
}

const switchOff = async ()=>{
  const switch1 = document.querySelector('img.switch[data-id="1"]')
  const switch2 = document.querySelector('img.switch[data-id="2"]')
  const switch3 = document.querySelector('img.switch[data-id="3"]')
  const switch4 = document.querySelector('img.switch[data-id="4"]')
  setTimeout(()=>{
    if(switch1){
      switch1.className = 'switch active'
    }
    if(switch2){
      switch2.className = 'switch'
    }
    if(switch3){
      switch3.className = 'switch'
    }
    if(switch4){
      switch4.className = 'switch'
    }
  },100)
  setTimeout(()=>{
    if(switch2){
      switch2.className = 'switch active'
    }
    if(switch1){
      switch1.className = 'switch'
    }
    if(switch3){
      switch3.className = 'switch'
    }
    if(switch4){
      switch4.className = 'switch'
    }
  },200)
  setTimeout(()=>{
    if(switch3){
      switch3.className = 'switch active'
    }
    if(switch2){
      switch2.className = 'switch'
    }
    if(switch1){
      switch1.className = 'switch'
    }
    if(switch4){
      switch4.className = 'switch'
    }
  },300)
  setTimeout(()=>{
    if(switch4){
      switch4.className = 'switch active'
    }
    if(switch2){
      switch2.className = 'switch'
    }
    if(switch3){
      switch3.className = 'switch'
    }
    if(switch1){
      switch1.className = 'switch'
    }
  },400)
}

const switchOn = async ()=>{
  const switch1 = document.querySelector('img.switch[data-id="1"]')
  const switch2 = document.querySelector('img.switch[data-id="2"]')
  const switch3 = document.querySelector('img.switch[data-id="3"]')
  const switch4 = document.querySelector('img.switch[data-id="4"]')
  setTimeout(()=>{
    if(switch4){
      switch4.className = 'switch active'
    }
    if(switch2){
      switch2.className = 'switch'
    }
    if(switch3){
      switch3.className = 'switch'
    }
    if(switch1){
      switch1.className = 'switch'
    }
  },100)
  setTimeout(()=>{
    if(switch3){
      switch3.className = 'switch active'
    }
    if(switch1){
      switch1.className = 'switch'
    }
    if(switch2){
      switch2.className = 'switch'
    }
    if(switch4){
      switch4.className = 'switch'
    }
  },200)
  setTimeout(()=>{
    if(switch2){
      switch2.className = 'switch active'
    }
    if(switch4){
      switch4.className = 'switch'
    }
    if(switch1){
      switch1.className = 'switch'
    }
    if(switch3){
      switch3.className = 'switch'
    }
  },300)
  setTimeout(()=>{
    if(switch1){
      switch1.className = 'switch active'
    }
    if(switch2){
      switch2.className = 'switch'
    }
    if(switch3){
      switch3.className = 'switch'
    }
    if(switch4){
      switch4.className = 'switch'
    }
  },400)
}

const armPlay = () =>{
  const arm = document.querySelector("img.arm")
  if(arm)
  {
    armPlaying.value = true
  }
}

const armStop = async () =>{
  const arm = document.querySelector("img.arm")
  if(arm)
  {
    const trans = window.getComputedStyle(arm).transform
    armPlaying.value = false
    armPlayed.value = true
    transFromStyle.value = trans
    await nextTick()
    setTimeout(()=>{
      armPlayed.value = false
    },1)
  }
}


</script>

<template>
  <div class="left-box">
    <img class="base" src="@/assets/res/record_player_dark_base.png" alt="" />
    <img class="arm" :class="{playing:armPlaying,played:armPlayed}" src="@/assets/res/record_player_dark_arm.png" alt="" />
    <img class="switch" data-id="1" src="@/assets/res/record_player_dark_switch_img_1.png"  alt="" />
    <img class="switch" data-id="2" src="@/assets/res/record_player_dark_switch_img_2.png"  alt="" />
    <img class="switch" data-id="3" src="@/assets/res/record_player_dark_switch_img_3.png"  alt="" />
    <img class="switch active" data-id="4" src="@/assets/res/record_player_dark_switch_img_4.png" alt="" />
    <img class="record" src="@/assets/res/record_player_record_lp_base.png" alt="" />
    <img class="record_higher" src="@/assets/res/record_player_record_lp_highlight.png" alt="" />
    <img class="album_cover" :src="audioPlayer.playingSong && audioPlayer.playingSong.album_cover? audioPlayer.playingSong.album_cover :Logo" alt="" />
    <img class="sound_effect" src="@/assets/res/record_player_dark_soundeffect_img.png" alt="">
    <img class="sound_effect_btn" src="@/assets/res/record_player_dark_soundeffect_btn_on.png" alt="">
  </div>
</template>

<style scoped lang="scss">
.left-box{
  height: 405px;
  width: 419px;
  position: relative;

  img.base{
    height: 405px;
    width: 419px;
  }

  img.arm{
    position: absolute;
    height:420px;
    right: 6px;
    top: -43px;
    z-index: 800;
    transform-origin: 62px 92px;
    transform: rotate(0deg);
    transition: all 1s;

    &.playing{
      transform: rotate(15deg);
      animation-delay: 1s;
      animation-duration: 3s;
      animation-direction: alternate;
      animation-iteration-count: infinite;
      animation-timing-function: linear;
      animation-fill-mode: forwards;
      animation-name: armPlay;
    }

    &.played{
      transform: v-bind(transFromStyle);
    }

  }

  img.sound_effect{
    position: absolute;
    height: 70px;
    bottom: 35px;
    left: 30px;
    transform: rotate(-25deg);
  }

  img.sound_effect_btn{
    position: absolute;
    height: 18px;
    bottom: 100px;
    left: 24px;
    //transform: rotate(-25deg);
  }

  img.switch{
    position: absolute;
    height: 60px;
    //width: 25px;
    right: 20px;
    bottom: 30px;
    z-index: 1;
    display: none;
    &.active{
      z-index: 1000;
      display: block !important;
    }
  }

  img.record{
    position: absolute;
    border-radius: 50%;
    height: 340px;
    z-index: 50;
    right: 50%;
    top: 50%;
    transform: translate(50%,-50%);
    animation: play 9s linear 1s infinite;
  }

  img.record_higher{
    position: absolute;
    border-radius: 50%;
    height: 340px;
    z-index: 52;
    right: 50%;
    top: 50%;
    transform: translate(50%,-50%);
    animation: record_highlight_Play 2s linear infinite alternate;
  }

  img.album_cover{
    position: absolute;
    border: 20px solid #2c2c2c;
    border-radius: 50%;
    height: 213px;
    right: 50%;
    top: 50%;
    z-index: 500;
    transform: translate(50% , -50%);
    animation: play 9s linear 1s infinite;
  }


  @keyframes play {
    0%{
      transform: translate(50%,-50%) rotate(0deg);
    }
    100%{
      transform: translate(50%,-50%) rotate(360deg);
    }
  }

  @keyframes armPlay {
    0%{
      transform: rotate(15deg);
    }
    100%{
      transform: rotate(20deg);
    }
  }

  @keyframes record_highlight_Play {
    0%{
      transform: translate(50%,-50%) rotate(0deg);
    }
    100%{
      transform: translate(50%,-50%) rotate(5deg);
    }
  }

}
</style>